package com.weixue.controller.mall;

import com.weixue.common.Constants;
import com.weixue.common.IndexConfigTypeEnum;
import com.weixue.controller.vo.NongFuMallIndexCarouselVO;
import com.weixue.controller.vo.NongFuMallIndexCategoryVO;
import com.weixue.controller.vo.NongFuMallIndexConfigGoodsVO;
import com.weixue.service.NongFuMallCarouselService;
import com.weixue.service.NongFuMallCategoryService;
import com.weixue.service.NongFuMallIndexConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Day32
 *      首页制作
 */
@Controller
public class IndexController {

    @Resource
    private NongFuMallCarouselService nongFuMallCarouselService;

    @Resource
    private NongFuMallIndexConfigService nongFuMallIndexConfigService;

    @Resource
    private NongFuMallCategoryService nongFuMallCategoryService;

    /**
     * Day32
     *      页面跳转到mall/index.html
     * Day33
     *      首页商品数据读取
     */
    @GetMapping({"/index", "/", "/index.html"})
    public String indexPage(HttpServletRequest request) {
        List<NongFuMallIndexCategoryVO> categories = nongFuMallCategoryService.getCategoriesForIndex();
        if (CollectionUtils.isEmpty(categories)) {
            return "error/error_5xx";
        }
        List<NongFuMallIndexCarouselVO> carousels = nongFuMallCarouselService.getCarouselsForIndex
                (Constants.INDEX_CAROUSEL_NUMBER);
        List<NongFuMallIndexConfigGoodsVO> hotGoodses = nongFuMallIndexConfigService.getConfigGoodsesForIndex
                (IndexConfigTypeEnum.INDEX_GOODS_HOT.getType(), Constants.INDEX_GOODS_HOT_NUMBER);
        List<NongFuMallIndexConfigGoodsVO> newGoodses = nongFuMallIndexConfigService.getConfigGoodsesForIndex
                (IndexConfigTypeEnum.INDEX_GOODS_NEW.getType(), Constants.INDEX_GOODS_NEW_NUMBER);
        List<NongFuMallIndexConfigGoodsVO> recommendGoodses = nongFuMallIndexConfigService.getConfigGoodsesForIndex
                (IndexConfigTypeEnum.INDEX_GOODS_RECOMMOND.getType(), Constants.INDEX_GOODS_RECOMMOND_NUMBER);
        request.setAttribute("categories", categories);//分类数据
        request.setAttribute("carousels", carousels);//轮播图
        request.setAttribute("hotGoodses", hotGoodses);//热销商品
        request.setAttribute("newGoodses", newGoodses);//新品
        request.setAttribute("recommendGoodses", recommendGoodses);//推荐商品
        return "mall/index";
    }
}
